package aipay.mobile.activity;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Scanner;

import aipay.mobile.SQLite.DatabaseHandler;
import aipay.mobile.model.Acount;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.AutoCompleteTextView;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class Login extends ActionBarActivity implements OnClickListener {
	private Button login,register;
	private EditText pass;
	private AutoCompleteTextView userlogin;
	private TextView forgetPass;
	private CheckBox saveUser;
	private DatabaseHandler db;
	private int check=0;
	private ArrayList<Acount> listlistAcount;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		this.requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.activity_login);
		Initilization();
		db = new DatabaseHandler(this);
		listlistAcount = (ArrayList<Acount>) db.getAllAcount();
		
		login.setOnClickListener(this);
		register.setOnClickListener(this);
		forgetPass.setOnClickListener(this);
		saveUser.setOnClickListener(this);
	}

	private void Initilization() {
		// TODO Auto-generated method stub
		login = (Button) findViewById(R.id.login);
		register = (Button) findViewById(R.id.register);
		forgetPass = (TextView) findViewById(R.id.forgetPass);
		saveUser = (CheckBox) findViewById(R.id.saveUser);
		userlogin = (AutoCompleteTextView) findViewById(R.id.userlogin);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.login:
			Intent i2 = new Intent(Login.this,Home.class);
			startActivity(i2);
//			if(userlogin.getText().toString().equals(""))
//			{
//				Toast.makeText(getApplicationContext(), "Bạn chưa nhập tên đăng nhập", Toast.LENGTH_SHORT).show();
//			}
//			else {
//				if (pass.getText().toString().equals("")) {
//					Toast.makeText(getApplicationContext(), "Bạn chưa nhập mật khẩu", Toast.LENGTH_SHORT).show();
//				}
//				else {
////					if (CheckLogin()==1) {
//						Intent i = new Intent(Login.this,Home.class);
//						startActivity(i);
////						Toast.makeText(getApplicationContext(),"Đăng nhập thành công",Toast.LENGTH_SHORT).show();
////					}
////					else {
////						Toast.makeText(getApplicationContext(),"Đăng nhập không thành công",Toast.LENGTH_SHORT).show();
////					}
//					
//				}
//			}
			break;
        case R.id.register:
        	Intent i = new Intent(Login.this,Register.class);
        	startActivity(i);
			break;
        case R.id.forgetPass:
			break;
        case R.id.saveUser:
        	if(saveUser.isChecked())
        	{
        		check = 1;
        		Toast.makeText(this, "true",Toast.LENGTH_SHORT).show();
        	}
        	else {
        		check =0;
        		Toast.makeText(this,"false",Toast.LENGTH_SHORT).show();
			}
			break;

		default:
			break;
		}
	}

	private int CheckLogin() {
		Boolean kt = false;
		for (int i = 0; i < listlistAcount.size(); i++) {
			if (listlistAcount.get(i).getUser().equals(userlogin.getText().toString())) {
				kt = true;
				break;
			}
		}
		if (kt) {
			
		}
		else {
			db.addAcount(new Acount(userlogin.getText().toString(), check,0,null,null,null));
		}
		return 1;
	}
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event)  {
	    if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
	        // do something on back.
	    	Intent intent = new Intent(Intent.ACTION_MAIN);
	    	intent.addCategory(Intent.CATEGORY_HOME);
	    	intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
	    	startActivity(intent);
	    }

	    return super.onKeyDown(keyCode, event);
	}
}
